package com.me.study.JVM;

import java.text.MessageFormat;

public class TestMemory {

    private static final MessageFormat format = new MessageFormat("当前应用内存信息：\n" +
            "free：{0}M\n" +
            "current total（随着扩容变大）：{1}M \n" +
            "max：{2}M");

    public static void main(String[] args) {
        System. out .println(toMemoryInfo());
    }

    /**
      * 获取当前 jvm 的内存信息
      */
    public static String toMemoryInfo() {
        Runtime runtime = Runtime.getRuntime();
        int nFreeMemory = (int) (runtime.freeMemory() / 1024 / 1024);
        int nTotalMemory = (int) (runtime.totalMemory() / 1024 / 1024);
        int nMaxMemory = (int) (runtime.maxMemory() / 1024 / 1024);

        return format.format(new Object[]{nFreeMemory, nTotalMemory, nMaxMemory});

    }

} 
